﻿package src.components 
{
	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	// controls
	import fl.controls.Button;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.controls.Slider;
	import fl.events.SliderEvent;
	import fl.data.DataProvider;
	import fl.controls.ScrollPolicy;
	// standart
	import flash.utils.Dictionary;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.BitmapFilterQuality;
	// src	
	import src.components.BForm;
	import src.util.Utils;
	// astra
	import com.yahoo.astra.containers.formClasses.FormItem;	
	import com.yahoo.astra.layout.LayoutContainer;
	import com.yahoo.astra.layout.modes.BoxLayout;
	import com.yahoo.astra.containers.formClasses.FormLayoutStyle;
	import com.yahoo.astra.fl.managers.AlertManager;
	
	public class StockForm extends BForm {
	
		/**
		 * Each item of dataGrid dataProvidercontent following fields
		 * 		type - element type 
		 *		number - number of redundant elements
		 *		desc - description 
		 *		law - distribution law
		 *		recovery - recovery parametrs
		 *		stockId - id in stock
		 */
		private var dataGrid:DataGrid;
		private var gridFormItem;
		private var closeBtn;
		private var addBtn;
		private var editBtn;
		private var delBtn;
		private var currentId = 1;		
		
		public function StockForm() {
			super("Склад");
		}
		
		protected override function initUI() {
			// grid		
			this.dataGrid = new DataGrid();
			this.dataGrid.setSize(300, 300);
			var typecolumn:DataGridColumn = new DataGridColumn("type");
			typecolumn.headerText = "Тип";
			typecolumn.minWidth = 200;
			typecolumn.resizable = true;		
			this.dataGrid.addColumn(typecolumn);
			var numColumn:DataGridColumn = new DataGridColumn("number");
			numColumn.headerText = "Кол-во";
			numColumn.minWidth = 30;		
			this.dataGrid.addColumn(numColumn);		
			// buttons
			this.closeBtn = new Button();
			this.closeBtn.label = "Close";
			this.closeBtn.setSize(60,20);
			this.closeBtn.addEventListener(MouseEvent.CLICK, onCancel);
			
			this.addBtn = new Button();
			this.addBtn.label = "Add";
			this.addBtn.setSize(60,20);
			this.addBtn.addEventListener(MouseEvent.CLICK, onAddBtn);
			
			this.editBtn = new Button();
			this.editBtn.label = "Edit";
			this.editBtn.setSize(60,20);
			this.editBtn.addEventListener(MouseEvent.CLICK, onEdit);		
		
			this.delBtn = new Button();
			this.delBtn.label = "Delete";
			this.delBtn.setSize(60,20);
			this.delBtn.addEventListener(MouseEvent.CLICK, onDel);
		}
		
		protected override function initData() {
			//grid 
			gridFormItem = new FormItem("", this.dataGrid);
			gridFormItem.labelAlign = FormLayoutStyle.LEFT;
			gridFormItem.labelWidth = 0;
			
			var buttons = new FormItem("", [this.delBtn,this.editBtn, this.addBtn, this.closeBtn]);
			buttons.labelWidth = 40;
			
			var box : BoxLayout = new BoxLayout();
			box.direction = "vertical";
			box.verticalGap = 10;			
			var itemContainer : LayoutContainer = new LayoutContainer(box);	
			itemContainer.autoMask = false;
			itemContainer.x = itemContainer.y = 30;					
			itemContainer.addChild(gridFormItem);
			itemContainer.addChild(buttons);			
			this.form.addChild(itemContainer);
		}
		
		private function setMask(){
		var blur = new BlurFilter();			
			blur.quality = BitmapFilterQuality.HIGH;
			blur.blurX = 8;
			blur.blurY = 8;
			this.filters = [blur];
			this.mask = Utils.getMask(this.width, this.height);
			//var mask = Utils.getMask(this.width, this.height);
			//this.addChild(mask);
		}
		
		public function unmask() {
			this.filters = [];
			//this.removeChildAt(this.numChildren);
			this.mask = null;
		}
		
		private function onAddBtn(event) {
			var form = new ElementTypeForm();
			form.x = this.x;
			form.y = this.y;			
			var stageRef = this.stage;
			var stockForm = this;
			form.addEventListener(BForm.FORM_CLOSE, function(e) {				
				stockForm.unmask();
				stageRef.removeChild(form);
			});	
			form.addEventListener(BForm.FORM_OK, function(e) {				
				var data = form.getData();
				stockForm.dataGrid.addItem( {				
					type:data["name"],
					number:(data["number"]==null)?0:data["number"],
					desc:data["description"],
					law:data["law"],
					recovery:data["recovery"],
					stockId:stockForm.currentId
				});
				stockForm.currentId++;
				stockForm.unmask();
				stageRef.removeChild(form);
			});	
			this.setMask();
			this.stage.addChild(form);
		}
		
		private function onEdit(event) {
			if (this.dataGrid.selectedIndex > -1 && this.dataGrid.dataProvider.length>0) {				
				var item = this.dataGrid.getItemAt(this.dataGrid.selectedIndex);
				var dict = new Dictionary();
				dict["name"] = item.type;
				dict["description"] = item.desc;
				dict["law"] = item.law; 
				dict["recovery"] = item.recovery;
				dict["number"] = item.number;
				var form = new ElementTypeForm();
				form.setData(dict);
				form.x = this.x;
				form.y = this.y;			
				var stageRef = this.stage;
				var stockForm = this;
				form.addEventListener(BForm.FORM_CLOSE, function(e) {				
					stockForm.unmask();
					stageRef.removeChild(form);
				});	
				form.addEventListener(BForm.FORM_OK, function(e) {				
					var data = form.getData();
					stockForm.dataGrid.dataProvider.replaceItem( {				
						type:data["name"],
						number:(data["number"]==null)?0:data["number"],
						desc:data["description"],
						law:data["law"],
						recovery:data["recovery"],
						stockId:item.stockId
					}, item);
					stockForm.unmask();
					stageRef.removeChild(form);
				});	
				this.setMask();
				this.stage.addChild(form);
			}else {				
				AlertManager.createAlert(this, "Необходимо выделить элемент", "Ошибка"); 
			}
		}
		
		private function onDel(event) {			
			if (this.dataGrid.selectedIndex > -1 && this.dataGrid.dataProvider.length>0) {				
				this.dataGrid.removeItemAt(this.dataGrid.selectedIndex);
			}else {				
				AlertManager.createAlert(this, "Необходимо выделить элемент", "Ошибка"); 
			}
		}		
		
		public override function setData(dict:Dictionary) {
			if (dict["data"] != null) {
				trace(dict["data"]);
				this.dataGrid.dataProvider = dict["data"];
			}
		}
		
		public override function getData():Dictionary {
			var dict = new Dictionary();
			dict["data"] = this.dataGrid.dataProvider;
			return dict
		}
		
	}
	
}